<template>
	<view class="address-manage rf-row-wrapper">
		 <!-- <uni-section title="输入设备序列号，填写终端号码，完成绑定" type="line"></uni-section> -->
		<view class="row b-b">
			<text class="tit">姓名</text>
			<input
				class="input"
				type="text"
				v-model="cstmData.cstmName"
				placeholder="姓名"
				placeholder-class="placeholder"
			/>
		</view>
		<view class="row b-b">
			<text class="tit">腕表类别</text>
			<radio-group  class="input" @change="handleChange">
				<label class="invoice-manage">
					<radio value="1" checked /><text>HU-W-02</text>
				</label>
				<label>
					<radio value="2" /><text>sdcc腕表</text>
				</label>				
			</radio-group>
		</view>
		<view class="row b-b">
			<text class="tit">设备编号</text>
			<input class="input" type="text" v-model="cstmData.imieCode" placeholder="输入设备序列号或扫码获取" placeholder-class="placeholder" />
			<text class="cell-more iconfont iconsaomiao" @tap.stop="handleSaoMiao"></text>
		</view>
		<view class="row b-b">
			<text class="tit">终端号码</text>
			<input
				class="input"
				type="text"
				v-model="cstmData.terminalNumber"
				placeholder="请输入终端号码"
				placeholder-class="placeholder"
			/>
		</view>

		<button
			class="add-btn"
			:class="'bg-' + themeColor.name"
			:disabled="btnLoading"
			:loading="btnLoading"
			@tap="confirm"
		>
			提交
		</button>

		<!--加载动画-->
		<rfLoading isFullScreen :active="loading"></rfLoading>
	</view>	
	
	
</template>

<script>
	// #ifdef H5
	import jweixin from '@/common/jweixin';
	// #endif
	import { saveWatch } from '@/api/userInfo'
    export default {
    	 data() {
    		return {
				cstmData:{
					cstmName : '',
					terminalNumber : '',
					watchType : '1',
					imieCode : '',
					cstmId : ''
				},
				btnLoading: false,
				loading :false
    		}
    	},
		onLoad(option) {
    	  this.cstmData.cstmId = option.id;
		  this.cstmData.cstmName = option.name;
		  
    	},
		methods: {
			//扫一扫
			handleSaoMiao(){
			const _this = this;
			/*  #ifndef H5  */
			uni.scanCode({
				success: function (res) {
					/*
					_this.$http.get(`${saveWatch}`, this.cstmData).then(() => {
						if(r.data.success){
						  _this.$mHelper.toast('绑定成功');
						}else{
							_this.$mHelper.toast(`绑定失败${r.data.msg}`);
						}

					}).catch(err => {
						_this.$mHelper.toast(err);
					});*/
					this.cstmData.imieCode = res.result;
				},
				fail(res) {
					// 支付成功后的回调函数
					_this.$mHelper.toast('扫描失败：' + res.errMsg);
				}
			});
			/*  #endif  */
			/*  #ifdef H5  */
			if (this.$mPayment.isWechat()) {
				jweixin.ready(() => {
					jweixin.scanQRCode({
						needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
						success(res) {
							/*
							_this.$http.get(saveWatch, this.cstmData).then(() => {
								if(r.data.success){
								  _this.$mHelper.toast('绑定成功');
								}else{
									_this.$mHelper.toast(`绑定失败${r.data.msg}`);
								}
							});*/
							this.cstmData.imieCode = res.result;					
							
						},
						fail(res) {
							// 支付成功后的回调函数
							_this.$mHelper.toast('扫描失败：' + res.errMsg);
						}
					});
				});
			} else {
				this.$mHelper.toast('请在微信H5进行相关操作');
			}
			/*  #endif  */				
			},
			handleChange(e){
				console.log(e.detail.value)
				this.cstmData.watchType = e.detail.value;
			},
    	   async confirm() {
			   var params = {
                        cstmId : this.cstmData.cstmId,
                        imieCode : this.cstmData.imieCode,
                        terminalNumber : this.cstmData.terminalNumber,
                        watchType : this.cstmData.watchType
                   }
				   console.log("绑定腕表请求参数: " + JSON.stringify(params));
				this.btnLoading = true;
    			await this.$http
    				.get(`${saveWatch}`, params)
    				.then(r => {
						console.log("绑定腕表返回: " + JSON.stringify(r.data));
    					if(r.data.success){
							this.loading = false;
    						this.btnLoading = false;
    						this.$mHelper.toast('绑定腕表成功！');
    						this.$mRouter.push({
								route : `/pages/zhyl/device/watch/watch?id=${this.cstmData.id}`
							})
    					}else{
    						this.btnLoading = false;
							this.loading = false;
    						this.$mHelper.toast(r.data.msg);
    					}
    			
    				})
    				.catch(() => {
    					this.btnLoading = false;
						this.loading = false;
    				});
    		}
    	},
    }
</script>

<style lang="scss">
page {
	background: $page-color-base;
}
.address-manage {
	padding-top: 16upx;
}
</style>
